package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        // 构建查询条件 根据用户ID查询
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCuttendId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> car(@RequestBody ShoppingCart shoppingCart) {
//        业务逻辑
//
//        1、先设置用户id  指定好该购物车是哪个用户的
//
        Long userId = BaseContext.getCuttendId();
        shoppingCart.setUserId(userId);
//        2、在添加前 先查询该用户当前表中是否有该套餐或者菜品
//
//        判断
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        用户名相同是前提条件
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
//        如果传过来的是菜品就查菜，不是就只有套餐的选择了
        if (shoppingCart.getDishId() != null) {
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
//        查出当前用户的购物车信息
//        在添加前 添加更改信息
        shoppingCart.setCreateTime(LocalDateTime.now());
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);

//		如果有就只需数量加1
//
        if (one != null) {
            int number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //​		没有则添加到购物车中，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }


//    减少数量功能


    @PostMapping("/sub")
    public R<ShoppingCart> dele(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCuttendId());

        //        证明传过来的是dish
        if(shoppingCart.getDishId() !=null) {
//        菜品与套餐分别判断
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
//        按条件查询出来结果
            ShoppingCart dishOne = shoppingCartService.getOne(lambdaQueryWrapper);
//            这里需要先减1 然后判断是否越界
            dishOne.setNumber(dishOne.getNumber() - 1);
            Integer DishNumber = dishOne.getNumber();
            if (DishNumber>0) {
                shoppingCartService.updateById(dishOne);
            }
                else if (DishNumber == 0) {
shoppingCartService.removeById(dishOne);
                }
                else if(DishNumber<0){
                    return R.error("减错误异常");
            }
            return R.success(dishOne);

        }
        //        证明传过来的是Setmeal
        if(shoppingCart.getSetmealId() !=null) {
//        菜品与套餐分别判断
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        按条件查询出来结果
            ShoppingCart SetmealOne = shoppingCartService.getOne(lambdaQueryWrapper);
            shoppingCart.setNumber(SetmealOne.getNumber() - 1);
            Integer DishNumber = SetmealOne.getNumber();
            if (DishNumber>0) {
                shoppingCartService.updateById(SetmealOne);
            }
            else if (DishNumber == 0) {
                shoppingCartService.removeById(SetmealOne);
            }
            else if(DishNumber<0){
                return R.error("减错误异常");
            }
            return R.success(SetmealOne);
        }
        //如果两个大if判断都进不去
        return R.error("操作异常");
    }
    @DeleteMapping("/clean")
    public R<String> clean(ShoppingCart shoppingCart){
//        先判断用户id  根据id删除该购物表数据
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCuttendId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空");
    }
}
